<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\SignUpJobPosition;
use App\Models\JobpositionModel;
use App\Models\User as UserModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Table;

class SignUpJobPositionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new SignUpJobPosition(['userInfo']), function (Grid $grid) {
            $job_position_id = request()->get('job_position_id');
            $grid->model()->when($job_position_id > 0, function ($query) use ($job_position_id) {
                $query->where('job_position_id', $job_position_id);
            })->orderByDesc('id');
            $grid->column('id');
            $grid->combine('用户信息', ['created_at', 'userInfo.name', 'userInfo.gender', 'age', 'userInfo.mobile']);
            $grid->combine('职位信息', ['position_name', 'contact_type', 'contact','contact_img']);


            $grid->column('created_at', '报名时间')->sortable();
            $grid->column('userInfo.name', '用户姓名');
            $grid->column('userInfo.gender', '性别')->using(UserModel::GENDER_LIST)->dot(
                [
                    1 => 'primary',
                    2 => 'pink',
                    3 => 'success',
                ],
            );
            $grid->column('age', '年龄')->display(function () {
                return $this->userInfo->age();
            });
            $grid->column('userInfo.mobile', '联系方式');
            $grid->column('position_name');
            $grid->column('contact_type')->using(JobpositionModel::CONTACT_TYPE_LIST);

            $grid->column('contact', '报名方式')->display(function () {
                $info = '';
                if ($this->contact_type == JobpositionModel::CONTACT_MOBILE) {
                    $info = $this->phone_number;
                }
                if ($this->contact_type == JobpositionModel::CONTACT_QQ) {
                    $info = "$this->qq_number";
                }
                if ($this->contact_type == JobpositionModel::CONTACT_WECHAT) {
                    $info = "$this->wechat_number";
                }
                return $info;
            });
            $grid->column('contact_img', '二维码')
                ->display(function () {
                    $img = '';
                    if ($this->contact_type == JobpositionModel::CONTACT_QQ) {
                        $img = $this->qq_qr_code;
                    }
                    if ($this->contact_type == JobpositionModel::CONTACT_WECHAT) {
                        $img = $this->wechat_qr_code;
                    }
                    if ($img) {
                        return $img;
                    }
                })->image('',100,100);
            $grid->filter(function (Grid\Filter $filter) {
                $filter->expand();
                $filter->panel();
                $filter->equal('user_id', '用户ID')->width(2);
                $filter->like('userInfo.name', '用户姓名')->width(2);
                $filter->equal('userInfo.mobile', '联系方式')->width(2);
            });
//            $grid->order->orderable(); // 开启排序功能
            // 禁用过滤器按钮
            $grid->disableFilterButton();
            // 禁用批量删除按钮
            $grid->disableBatchDelete();
            $grid->disableActions();
            $grid->disableCreateButton();
            // 禁用显示按钮
            $grid->disableViewButton();
            $grid->disableRowSelector();
        });
    }
}
